#!/usr/bin/env bash

set -e -u -x

mkdir -p /etc/api-umbrella
{
  echo "app_env: development"
  echo "http_port: $HTTP_PORT"
  echo "https_port: $HTTPS_PORT"
  echo "log:"
  echo "  destination: console"
  echo "postgresql:"
  echo "  password: dev_password"
  echo "  migrations:"
  echo "    password: dev_password"
} > /etc/api-umbrella/api-umbrella.yml

mkdir -p /build/.task
ln -snf /build/.task /app/.task

mkdir -p /build/build/work/stage/opt/api-umbrella/var/log
ln -snf /build/build/work/stage/opt/api-umbrella /opt/api-umbrella
ln -snf /build/build/work/stage/opt/api-umbrella/var/log /var/log/api-umbrella
ln -snf /build/build/work /app/build/work

mkdir -p /build/test/tmp/run
mkdir -p /app/test/tmp
ln -snf /build/test/tmp/run /app/test/tmp/run

mkdir -p /app/build/work/tasks/app-deps/admin-ui/pnpm/_persist/node_modules
ln -snf /app/build/work/tasks/app-deps/admin-ui/pnpm/_persist/node_modules /app/src/api-umbrella/admin-ui/node_modules

mkdir -p /app/build/work/tasks/app-deps/example-website/pnpm/_persist/node_modules
ln -snf /app/build/work/tasks/app-deps/example-website/pnpm/_persist/node_modules /app/src/api-umbrella/example-website/node_modules

mkdir -p /app/build/work/tasks/app-deps/web-app/pnpm/_persist/node_modules
ln -snf /app/build/work/tasks/app-deps/web-app/pnpm/_persist/node_modules /app/src/api-umbrella/web-app/node_modules

mkdir -p /app/build/work/tasks/test-deps/bundle/_persist/.bundle
ln -snf /app/build/work/tasks/test-deps/bundle/_persist/.bundle /app/.bundle

mkdir -p /app/src/api-umbrella/admin-ui/tmp
chmod 1777 /app/src/api-umbrella/admin-ui/tmp

mkdir -p ~/.config/pnpm
printf "update-notifier=false\nstore-dir=%s/store\n" "$PNPM_HOME" > ~/.config/pnpm/rc

exec "$@"
